#!/usr/bin/perl
use utf8;
use strict;
use Data::Dumper;

$Data::Dumper::Useqq = 1;{
 no warnings 'redefine';
 sub Data::Dumper::qquote {
   my $s = shift;
   return "'$s'";
 }
}

open (FILE, "out.txt") or die "Sorry, but i can't open 'out.txt'";
binmode(FILE, ":encoding(utf-8)");
binmode(STDOUT, ":encoding(utf-8)");
binmode(STDERR, ":encoding(utf-8)");
open (U_NAMES, '>dicts/u_names.txt') or die;
binmode(U_NAMES, ":encoding(utf-8)");
open (CITY, '>dicts/city.txt') or die;
binmode(CITY, ":encoding(utf-8)");


my @s=<FILE>;
my %m;
my %f;
my $i;
my $line;
my %city;
my %norm;
my %c_norm;
my $c;
my $a2;
my %unk;
my %p;
my %n;
my %u_names;


for ($line = 0; $line < scalar(@s) ; $line++ ){
	if ($i = index($s[$line], "City_check") !=-1)
	{
		$c = $line + 1;
                $s[$c] =~ s/"//g;
		$i = index ($s[$c], "= ");
		$a = substr ($s[$c], $i+2,length($s[$c])-$i-3);
		$city{lc($a)} += 1;
		
	}

	if ($i = index($s[$line], "Pers_check") !=-1)
        {
                $c = $line + 1;
                $s[$c] =~ s/"//g;
                $i = index ($s[$c], "= ");
                $a = substr ($s[$c], $i+2,length($s[$c])-$i-3);
                $p{lc($a)} += 1;

        }


	if ($i = index($s[$line], "Male") != -1)
        {
	       $s[$line] =~ s/"//g;
               $i = index ($s[$line], "= ");
               $a = substr ($s[$line], $i+2,length($s[$line])-$i-3);
	       $norm{lc($a)} += 1 ;
		     
        }
	

	if ($i = index($s[$line], "Undef") != -1)
        {
                $s[$line] =~ s/"//g;
                $i = index ($s[$line], "= ");
                $a = substr ($s[$line], $i+2,length($s[$line])-$i-3);
               # $c = $line + 1;
               # $s[$c] =~ s/"//g;
               # $i = index ($s[$c], "= ");
               # $a2 = substr ($s[$c], $i+2,length($s[$c])-$i-3);

                 $unk{lc($a)} += 1;
        }
	
	if ($i = index($s[$line], "Uname") != -1)
        {
                $s[$line] =~ s/"//g;
                $i = index ($s[$line], "= ");
                $a = substr ($s[$line], $i+2,length($s[$line])-$i-3);
                $u_names{lc($a)} += 1;
        }


	
}

my %l;
my @sort = sort {$city{$b} <=> $city{$a}} keys %city;
my $el;
foreach $el (@sort){
	if ($city{$el} > $p{$el}){ 
		print CITY "$el\n";
		$l{$el} +=1;			
	}
}


@sort = sort keys %norm;
foreach $el (@sort){

#print $el, " => ", $norm{$el}, "\n";
}

my @sort = sort {$norm{$b} <=> $norm{$a}} keys %norm;
my $el;
foreach $el (@sort){
     
        print $el, " => ", $norm{$el}, "\n";
}

print "\n";
foreach $el (keys %u_names){

 if (!$l{$el}){
  print U_NAMES "$el\n";}	 
}

#print Dumper(\%persn);

